import {
	baseURL,
	uploadPath
} from '@api/api.js';
import {
	user
} from '@utils/request.js'
/*
 * @type 1 上传选取过的临时地址  2 选择文件后上传
 * @e 上传后的事件
 */
export default function(type, e) {
	if (user) {
		if (type == 1&&e.length) {
			return new Promise((resolve, reject) => {
				uni.uploadFile({
					url: baseURL + uploadPath,
					filePath: e[e.length-1].url,
					name: 'file',
					header: {
						token: user.token
					},
					success(uploadFileRes) {
						let info = JSON.parse(uploadFileRes.data);
						resolve(info)
					},
					fail(err) {
						reject(err)
					}
				});
			})

		} else if (type == 2) {
			return new Promise((resolve, reject) => {
				uni.chooseImage({
					count: 1,
					success(tempFilePath) {
						uni.uploadFile({
							url: baseURL + uploadPath,
							filePath: tempFilePath.tempFilePaths[0],
							name: 'file',
							header: {
								token: user.token
							},
							success(uploadFileRes) {
								let info = JSON.parse(uploadFileRes.data);
								resolve(info)
							},
							fail(err) {
								reject(err)
							}
						});
					},
					fail(err) {
						reject('选取失败')
					}
				})
			})
		}
	} else {
		return uni.showToast({
			title: '登陆后操作',
			icon: 'none'
		})
	}
}